package com.imay.videorecord.fragment;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.imay.videorecord.R;
import com.imay.videorecord.view.load.ProgressWheel1;


/**
 * Created by Administrator on 2017/2/4.
 */

public class LoadingDialogFragment extends DialogFragment{

    private static LoadingDialogFragment dialog;
    private View view;
    private ProgressWheel1 progressWheel;
    private boolean isShow = false;
    private TextView tv_load;

    /**
     * 三个状态0默认、1暂停、2完成
     */
    private int wasSpinning = 0;
    private int progressStatus;

    private int time = 500;

    final float perTextInt;
    float textCount;

    public LoadingDialogFragment() {
        perTextInt = 100f/90f;
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.FragmentLoadingDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        dialog.getDialog().setCanceledOnTouchOutside(false);
    }

    public static void show(Context context, FragmentManager fragmentManager,int time,OnLoadingListener loadingListener){
        if(dialog == null){
            dialog = new LoadingDialogFragment();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_progress_wheel1, null, false);
        ProgressWheel1 progressWheel = (ProgressWheel1) view.findViewById(R.id.progressBar);
        dialog.setProgressWheel(progressWheel);
        TextView tv_load = (TextView) view.findViewById(R.id.tv_load);
        dialog.setTv_load(tv_load);
        dialog.setOnLoadingListener(loadingListener);
        dialog.setView(view);

        if(!dialog.isShow){
            dialog.show(fragmentManager,LoadingDialogFragment.class.getSimpleName());
            if(dialog.getOnLoadingListener() != null){
                dialog.getOnLoadingListener().onShow();
            }
            dialog.wasSpinning = 0;
            dialog.during(time);
            dialog.initRunning();
            dialog.setCancelable(false);
            dialog.isShow = true;
        }
    }

    public void initRunning(){
        progressStatus = 0;
        textCount = 0;
        progressWheel.setProgress(0);
        progressWheel.setText("0%");
        new Thread(new Runnable() {
            public void run() {
                try {
                    while (progressStatus < 90) {
                        Thread.sleep(time);
                        // 默认情况
                        if(wasSpinning == 0 || wasSpinning == 2){
                            progressStatus = progressStatus+1;
                            textCount = perTextInt * progressStatus;
                            if(progressStatus<89 && progressStatus>75 && wasSpinning == 0){
                                time = time * 2;
                            }else if(progressStatus == 89 && wasSpinning == 0){
                                wasSpinning = 1;
                            }
                            mHandler.sendEmptyMessage(0);
                        }
                    }
                    if( progressStatus == 90){
                        mHandler.sendEmptyMessage(3);
                    }
                    Thread.sleep(500);
                    mHandler.sendEmptyMessage(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0: // 进度条回显
                    if(wasSpinning == 0 || wasSpinning == 2) {
                        progressWheel.incrementProgress(4);
                        progressWheel.setText((int)textCount + "%");
                    }
                    break;
                case 1:
                    progressWheel.setProgress(360);
                    progressWheel.setText("100%");
                    if(dialog != null && dialog.isShow){
                        if(dialog.getOnLoadingListener() != null){
                            dialog.getOnLoadingListener().onDimiss();
                        }
                        dialog.dismiss();
                        dialog.isShow = false;
                    }
                    break;
                case 3:
                    progressWheel.setProgress(360);
                    if(wasSpinning == 0 || wasSpinning == 2) {
                        progressWheel.setText((int)textCount + "%");
                    }
                    break;
            }
        }
    };

    public static void dimiss(){
        if(dialog != null){
            if(dialog.isShow){
                if(dialog.getOnLoadingListener() != null){
                    dialog.getOnLoadingListener().onDimiss();
                }
                dialog.dismiss();
            }
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        dialog.isShow = false;
    }



    public static void setWasSpinning(){
        dialog.setTime(30);
        dialog.wasSpinning = 2;
    }

    public static void during(int time){
        dialog.setTime(time);
    }

    private OnLoadingListener onLoadingListener;

    public void setOnLoadingListener(OnLoadingListener onLoadingListener) {
        this.onLoadingListener = onLoadingListener;
    }

    public OnLoadingListener getOnLoadingListener() {
        return onLoadingListener;
    }

    public interface OnLoadingListener {
        void onShow();
        void onDimiss();
    }

    public void setProgressWheel(ProgressWheel1 progressWheel) {
        this.progressWheel = progressWheel;
    }

    public void setView(View view) {
        this.view = view;
    }

    public void setTv_load(TextView tv_load) {
        this.tv_load = tv_load;
    }

    public void setTime(int time){
        this.time = time;
    }
}
